package com.yjj.im.Utils;

import android.content.Context;
import android.util.Log;
import android.view.View;


import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Map;

/**
 * Created by n on 2018/8/26.
 */

public class AFinalHttp {

    public static void request(String url, Object params, AjaxCallBack<? extends Object> callBack) {
        request(url, params,  callBack);
    }

    public static void request(String url, Object params, String phone, AjaxCallBack<? extends Object> callBack) {

        AjaxParams tempParams = new AjaxParams();
        tempParams.put("user", phone);
        tempParams.put("json", GsonUtil.toJson(params));

        FinalHttp fh = new FinalHttp();
        //设置Cookie
//        fh.addHeader("Phone", "13323190823");
//        fh.addHeader("Version", "1.1001");
//        fh.addHeader("Device", "android");

        fh.configTimeout(20000);
        fh.post(url, tempParams, callBack);
    }

    public static void request(String url, Object params, File file, AjaxCallBack<? extends Object> callBack) {

        AjaxParams tempParams = new AjaxParams();
        tempParams.put("json", GsonUtil.toJson(params));
        try {
            tempParams.put("img", file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        FinalHttp fh = new FinalHttp();
        //设置Cookie

        fh.configTimeout(20000);
        fh.post(url, tempParams, callBack);
    }

    public static void requestForGet(String url, Map<String, String> params, File file, AjaxCallBack<? extends Object> callBack){

        AjaxParams tempParams = new AjaxParams();
        FinalHttp fh = new FinalHttp();

        for (Map.Entry<String, String> entry : params.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            Log.d("AFianlHttp","key= " + key + " and value= " + value);
            tempParams.put(key,value);
        }


        //设置Cookie
        fh.configTimeout(20000);
        fh.get(url,callBack);
    }

    public static void requestForPost(String url, Map<String, String> params, File file, AjaxCallBack<? extends Object> callBack){

        AjaxParams tempParams = new AjaxParams();
        FinalHttp fh = new FinalHttp();
//        String timestamp = System.currentTimeMillis()+"";
//        tempParams.put("timestamp", timestamp);
//        tempParams.put("api_type", "2");
//        tempParams.put("pop_token", MD5Utils.getMD5String(timestamp+CApplication.api_secret));

        String geturl = tempParams.getParamString();
        tempParams = null;
        tempParams = new AjaxParams();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            Log.d("AFianlHttp","key= " + key + " and value= " + value);
            tempParams.put(key,value);
        }

        if(file != null){
            try {
                tempParams.put("file", file);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
        //设置Cookie
        fh.configTimeout(20000);
        fh.post(url+"?"+geturl,tempParams,callBack);
    }

    public static void requestByWX(String url, Object params, AjaxCallBack<? extends Object> callBack) {

        AjaxParams tempParams = new AjaxParams();
        tempParams.put("user", "wxlogin");
//        tempParams.put("json", AES.encodeByBase64(GsonUtil.toJson(params)));

        FinalHttp fh = new FinalHttp();
        //设置Cookie
//        fh.addHeader("Phone", "13323190823");
//        fh.addHeader("Version", "1.1001");
//        fh.addHeader("Device", "android");
//        fh.addHeader("Phone", "wxlogin");
//        fh.addHeader("Version", Utils.getVersionName(CApplication.context));
//        fh.addHeader("Device", "android");

        fh.configTimeout(20000);
        fh.post(url, tempParams, callBack);
    }
    public static abstract class CallBack extends AjaxCallBack<Object> {

        private Context context;
        private boolean showDialog = true;
        private View view;

        public CallBack(Context context) {
            this.context = context;
        }
        public CallBack(Context context, boolean showDialog) {
            this.context = context;
            this.showDialog = showDialog;
        }
        public CallBack(Context context, boolean showDialog, View view) {
            this.context = context;
            this.showDialog = showDialog;
            this.view = view;
        }
        @Override
        public void onStart() {
            if(showDialog){
                if(view != null){
                    view.setVisibility(View.VISIBLE);
                }
//                DialogUtil.showLoadDataDialog(context,view);
            }

        }
        @Override
        public void onLoading(long count, long current){
        }

        @Override
        public void onSuccess(Object o) {
            if(showDialog){
                if(view != null){
                    view.setVisibility(View.GONE);
                }
                DialogUtil.closeDialog();
            }

            success(o);
        }

        @Override
        public void onFailure(Throwable t, int errorNo, String strMsg) {
            if(showDialog){
                if(view != null){
                    view.setVisibility(View.GONE);
                }
//                DialogUtil.closeDialog();
            }

            Utils.toast("网络请求失败");
        }

        protected abstract void success(Object data);

        protected void failure(String msg) {
            if(showDialog){
                if(view != null){
                    view.setVisibility(View.GONE);
                }
                DialogUtil.closeDialog();
            }

            Utils.toast(msg);
        }
    }
}

